package com.android.nearshop.app.activity.map;

import android.location.Location;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;

import com.android.nearshop.R;
import com.android.nearshop.app.widget.TitleBar;
import com.android.nearshop.business.location.MyBMapManager;
import com.android.nearshop.business.location.MyBMapManager.OnLocationListener;
import com.android.nearshop.util.LocationUtils;
import com.android.nearshop.util.Util;
import com.baidu.mapapi.MKAddrInfo;
import com.baidu.mapapi.MapActivity;
import com.baidu.mapapi.MapController;
import com.baidu.mapapi.MapView;
import com.baidu.mapapi.MyLocationOverlay;

/*********************************************************
 * @author : zhaohua
 * @version : 2012-8-6
 * @see : 地图基类， 1.提供我的位置显示
 * @Copyright : copyrights reserved by personal 2007-2011
 **********************************************************/
public class MainMapActivity extends MapActivity implements OnLocationListener
{
    protected static final String TAG = "MainMapActivity";
    
    /**标题栏 */
    protected TitleBar mTitleBar;
    /**内容容器*/
    protected LinearLayout mContainer;
    
    protected MapController mMapController;
    protected MapView mMapView;
    private   MyLocationOverlay mLocationOverlay;
    protected Location mMyLocation;
    protected MKAddrInfo mMyAddress;
    protected MyBMapManager mMapManager;
    
    @Override
    protected void onCreate(Bundle arg0)
    {
        super.onCreate(arg0);
        mMapManager = MyBMapManager.getInstace(this);        
    }
    
    @Override
    public void setContentView(int layoutResID)
    {
        // 设置模板
        super.setContentView(R.layout.activity_template);
        // 初始化模拟成员
        initComponents();
        // 将内容放入容器
        View content = getLayoutInflater().inflate(layoutResID, null);
        mContainer.addView(content,new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        // 设置模板的标题
        setupTitleBar(mTitleBar);
        // 初始化地图元素
        initMapViews();
    }
    
    /**
     * 初始化三大块：头部的标题栏，中间的内容容器
     */
    private void initComponents()
    {
        mTitleBar = (TitleBar)findViewById(R.id.titleBar);   
        mContainer = (LinearLayout)findViewById(R.id.container);
    }
    
    /** 设置标题栏*/
    protected void setupTitleBar(TitleBar titleBar){}
    
    protected void reLocation(boolean updateable)
    {
        if(!updateable && mMapManager.getMyLoation() != null)
        {
            // 当前不用在请求
            mMyLocation = mMapManager.getMyLoation();
            mMyAddress =  mMapManager.getMyAddrInfo();
        }else
        {
            mMyLocation = null;
            mMyAddress = null;
            mMapManager.setOnLocationListener(this);
            mMapManager.startLocation(true);
        }
    }
     
    @Override
    protected void onResume()
    {
        try {
            super.initMapActivity(mMapManager.getBMapManager());
        }catch(Exception e)
        {
            // 避免重复初始化
        }
        mMapManager.resume();
        super.onResume();
    }

    @Override
    protected void onPause()
    {
        super.onPause();
    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();
    }

    @Override
    protected boolean isRouteDisplayed()
    {
        return false;
    }

    private void initMapViews()
    {     
        mMapManager.resume();
        super.initMapActivity(mMapManager.getBMapManager());
        this.mMapView = ((MapView) findViewById(R.id.bmapView));
        this.mMapView.setBuiltInZoomControls(true);
        this.mMapController = this.mMapView.getController();
        this.mMapController.setZoom(16);
        this.mMapView.setDrawOverlayWhenZooming(true);
    }

    protected void showMyPosition(boolean visilable)
    {
        showMyPosition(visilable, true);
    }
    
    protected void showMyPosition(boolean visilable, boolean isMoveto)
    {
        if(visilable)
        {
            if(mLocationOverlay == null)
            {
                this.mLocationOverlay = new MyLocationOverlay(this, this.mMapView);
                this.mMapView.getOverlays().add(this.mLocationOverlay);
            }
            mLocationOverlay.enableMyLocation();
            if(isMoveto)
            {
                mLocationOverlay.runOnFirstFix(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        moveToMyLocation();
                    }
                });
            }
        }else
        {
            if(mLocationOverlay != null)
            {
                mLocationOverlay.disableMyLocation();
            }
        }
    }
    
    protected void moveToMyLocation()
    {
        if(mLocationOverlay != null && mLocationOverlay.getMyLocation() != null)
        {
            LocationUtils.moveTo(mMapView, mLocationOverlay.getMyLocation(), true, false);
        }else
        {
            Util.showToast(this, R.string.locating);
        }
    }
    
    @Override
    public void onError()
    {
        mMyLocation = null;
        mMyAddress = null;
        Util.showToast(this, R.string.loc_not_found);
    }

    @Override
    public void onLocation(Location loc)
    {
        mMyLocation = loc;
        mMyAddress = null;
    }

    @Override
    public void onGetAddr(MKAddrInfo addr)
    {
        mMyAddress = addr;
    }
}
